import BaseOffice from "@/plugins/office/extend/base-office";
import globalMethods from "@/plugins/methods";

export default class OfdOffice extends BaseOffice{
  async view(transParams, zwIframe) {
    let offdiceTransIframe=zwIframe.document.getElementById('officeTransIframe');
    if(offdiceTransIframe){
      offdiceTransIframe.parentNode?.removeChild(offdiceTransIframe)
    }
    transParams.isLoadOfficeImmediate=true;
    transParams.notModifyWithSign = false; //不验章
    transParams.canEdit = false; //不可编辑
    await zwIframe.OfdAPI.initOfd({
      ...transParams,
      officecanSaveLocal: true,
      officecanPrint: true,
      needReadFile: true,
    });
    console.log('ofd 查询原文档')
  }
 /**
   * 编辑功能
   * @param bodyType
   * @param userId
   * @param zwIframe
   * @param canSign
   * @return {Promise<void>}
   */
 async editOffice({bodyType, userId, zwIframe, canSign}) {
  zwIframe.document.getElementById('viewState').value = '1';
  let transParams = this.getTransParams(zwIframe);
  if (transParams) {
      this.setOfficeStyle(zwIframe);
      //签章的判断
      if (!await this.validateSignature({...transParams, zwIframe})) {
        return;
      }
      //用转换的方式打开
      await this.editOfficeTrans({zwIframe, transParams, userId, canSign})
    }
  }

  async editOfficeTrans({zwIframe, transParams, userId,canSign}) {
    //ofd不支持office在线编辑
    return this.editNoOfficeTrans({zwIframe,canSign})
  }

  async editNoOfficeTrans({zwIframe,canSign}){
    var ofdTransParms = this.getTransParams(zwIframe);
    let needRepeatLoad=ofdTransParms.ofdReader==="dj"&&zwIframe.v3x.isCurrentSysWindows();
    ofdTransParms.canSign=canSign
    this.setOfficeUpdateFlag()
     //点聚多浏览器需要多次加载
    if(ofdTransParms.isLoadOfdFile&&!needRepeatLoad){
      ofdTransParms.canEdit = true;
      zwIframe.OfdAPI.setOptions(ofdTransParms);
      zwIframe.OfdAPI.modifyOfdContent();
    }else{
      ofdTransParms =  zwIframe.ofdTransParms;
      zwIframe.$("#officeTransIframe").remove();
      //优化下toolbar显示
      ofdTransParms.needShowToolBar=true;
      ofdTransParms.canEdit = true;
      ofdTransParms.notModifyWithSign = true; // 修改正文时，需要验章
      var zwIframeObj=document.getElementById("zwIframe").contentWindow;
      zwIframeObj.OfdAPI.initOfd(ofdTransParms)
    }
    console.log('ofd 修改文档')
  }

  async validateSignature({zwIframe}) {
    if (!zwIframe.OfdAPI?.hasSign) {
      return true
    }
    let hasSign = zwIframe.OfdAPI.hasSign();
    if (hasSign){
      globalMethods.warning({
        title: globalMethods.i18n('common.system.hint.label'),
        content: globalMethods.i18n('wpsplugin.ofd.hasSign'),
      });
      return false;
    }
    return true;
  }
  async viewOffice({bodyType, userId, zwIframe}) {
    let transParams = this.getTransParams(zwIframe);
    if (transParams) {
        await this.view(transParams, zwIframe);
      }
  }
   /**
   * 提取转换参数
   * @param ctx
   * @return {*}
   */
   getTransParams(ctx) {
    return ctx.OfdAPI.options;
  }
}

export async function checkOFDComplete({zwIframe}){

  let ofdAPI = zwIframe.OfdAPI;
  if (!ofdAPI){
    return false;
  }
  if (ofdAPI && ofdAPI.hasOpenOfdComplete()){
    return true;
  }
  let ofdLoadTimes=1;
  let checkResult = await new Promise(resolve => {
    let interval = setInterval(() => {
      if (ofdAPI && ofdAPI.hasOpenOfdComplete()) {
        clearInterval(interval);
        return resolve(true);
      }
      if (++ofdLoadTimes > 50) {
        clearInterval(interval);
        return resolve(false);
      }
    }, 1000);
  });
  
  return checkResult;
}
